<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018/11/6 0006
 * Time: 17:29
 */
namespace app\admin\model;
class Builder
{
    private $type = '';
    private $start = '';
    private $end = '';
    private $str = '';
    private $head = '';
    private $feild =[];
    private $right=[];
    private $first = '';
    private $last = '';
    /**快速创建的入口
     * @param 创建的类型 $str
     * @param null提交地址 $action
     * @param null样式 $class
     * @param null样式 $style
     * @param stringform表单提交的方式 $formType
     * @return Builder
     */
    static public function make($str,$action=null,$class=null,$style=null,$formType='post'){
        $obj = new Builder();
        $obj->type = $str;
        if($str=='form'){
            $obj->start = '<form action="'.$action.'" method="'.$formType.'" class="'.$class.'" style="'.$style.'" enctype="multipart/form-data">';
            $obj->end = '</form>';
        }elseif($str=='table'){
            $obj->start = '<table class="table table-border table-bordered table-bg '.$class.'" style="'.$style.'">';
            $obj->end   = '</table>';
        }
        return $obj;
    }

    /**添加头信息
     * @param array添加的类型 $type
     * @param array描述 $desc
     * @param array提交地址 $action
     * @param array样式 $class
     * @param array样式 $style
     * @return $this
     */
    public function addHead($type=[],$desc=[],$action=[],$class=[],$style=[]){
        $this->first .= '<div class="cl pd-5 bg-1 bk-gray mt-20"> 
		<span class="l">';
        foreach ($type as $key=>$value){
            if($value=='button alldel'){
                $this->first .= '<a href="javascript:;" onclick="allDel(\''.$action[$key].'\')" class="btn btn-danger radius '.$class[$key].'" style="'.$style[$key].'"><i class="Hui-iconfont">&#xe6e2;</i>'.$desc[$key].'</a> ';
            }elseif($value=='button add'){
                $this->first .= '<a href="'.$action[$key].'" class="btn btn-primary radius '.$class[$key].'" style="'.$style[$key].'"><i class="Hui-iconfont">&#xe600;</i> '.$desc[$key].'</a>';
            }
        }
        $this->first .= '</span></div>';
        return $this;
    }

    /**table的列
     * @param 字段名称 $name
     * @param 类型 $type
     * @param string描述 $desc
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addColumn($name,$type,$desc='',$class=null,$style=null){
        $this->feild[$name]['type'] = $type;
        $this->feild[$name]['class'] = $class;
        $this->feild[$name]['style'] = $style;
        $this->head .= "<td class='$class' style='$style'>$desc</td>";
        return $this;
    }

    /**添加表格右侧栏位
     * @param 字段名称 $desc
     * @param array类型 $type
     * @param array提交地址 $action
     * @param array $option
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addRight($desc,$type=[],$action=[],$option=[],$class=null,$style=null){
        $this->right['type'] = $type;
        $this->right['action'] = $action;
        $this->right['option'] = $option;
        $this->right['class'] = $class;
        $this->right['style'] = $style;
//        $this->right[] = $array;
        $this->head .= "<td class='$class' style='$style'>$desc</td>";
        return $this;
    }

    /**添加数据
     * @param array $array
     * @return $this
     */
    public function addData($array=[]){
//        return $this->feild;
        foreach ($array as $value){
            $this->str .='<tr>';
            if(isset($this->feild['INDEX'])&&$this->feild['INDEX']['type']=="checkbox"){
                $this->str .= '<td class="'.$this->feild['INDEX']['class'].'" style="'.$this->feild['INDEX']['style'].'"><input type="checkbox" value="'.$value['id'].'"></td>';;
            }
            foreach ($this->feild as $k=>$v){
                if(isset($value[$k])){
                    if($this->feild[$k]['type']=='text'){
                        $this->str.='<td class="'.$this->feild[$k]['class'].'" style="'.$this->feild[$k]['style'].'">'.$value[$k].'</td>';
                    }elseif ($this->feild[$k]['type']=='status'){
                        if($value[$k]==1){
                            $this->str.='<td class="'.$this->feild[$k]['class'].'" style="'.$this->feild[$k]['style'].'"><span class="label label-success radius">已启用</span></td>';
                        }else{
                            $this->str.='<td class="'.$this->feild[$k]['class'].'" style="'.$this->feild[$k]['style'].'"><span class="label radius">已禁用</span></td>';
                        }
                    }elseif ($this->feild[$k]['type']=='img'){
                        $this->str.='<td class="'.$this->feild[$k]['class'].'" style="'.$this->feild[$k]['style'].'"><img src="'.getFilePath($value[$k]).'" width="50"/></td>';
                    }
                }else{
                    if($this->feild[$k]['type']=='text'){
                        $this->str.='<td class="'.$this->feild[$k]['class'].'" style="'.$this->feild[$k]['style'].'"></td>';
                    }elseif ($this->feild[$k]['type']=='status'){
                            $this->str.='<td class="'.$this->feild[$k]['class'].'" style="'.$this->feild[$k]['style'].'"><span class="label radius">已禁用</span></td>';
                    }elseif ($this->feild[$k]['type']=='img'){
                        $this->str.='<td class="'.$this->feild[$k]['class'].'" style="'.$this->feild[$k]['style'].'"></td>';
                    }
                }
            }
            if($this->right){
                $this->str .= '<td class="'.$this->right['class'].'" style="'.$this->right['style'].'">';
                foreach ($this->right['type'] as $k=>$s){
                    if($s=='icon add'){
                        $this->str.='<a title="添加" href="'.$this->right['action'][$k].$value['id'].'" class="ml-5" style="text-decoration:none"><i class="layui-icon">&#xe654;</i></a>';
                    }elseif ($s=='icon edit'){
                        $this->str.='<a title="编辑" href="'.$this->right['action'][$k].$value['id'].'" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a>';
                    }elseif ($s=='icon del'){
                        $this->str.='<a title="删除" href="javascript:;" onclick="del(this,\''.$this->right['action'][$k].'\','.$value['id'].')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>';
                    }
                }
                $this->str.='</td>';
                $this->str .='</tr>';
            }
        }
        return $this;
    }

    /**想表单添加文本框
     * @param 字段名称 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param null样式 $class
     * @param null样式 $style
     * @param bool是否只读 $readonly
     * @return $this
     */
    public function addInput($name,$desc='',$value='',$class=null,$style=null,$readonly=false){
        if($readonly){
            $this->str .= '<div class="layui-row" style="margin-top: 15px;height: 50px">
			<label class="form-label col-xs-3 col-sm-3" style="text-align: right"><span class="c-red">*</span>'.$desc.'：</label>
			<div class="formControls col-xs-9 col-sm-9">
				<input type="text" class="input-text '.$class.'" style="margin-bottom: 15px;'.$style.'" autocomplete="off" value="'.$value.'" name="'.$name.'" readonly>
			</div>';
        }else{
            $this->str .= '<div class="layui-row" style="margin-top: 15px;height: 50px">
			<label class="form-label col-xs-3 col-sm-3" style="text-align: right"><span class="c-red">*</span>'.$desc.'：</label>
			<div class="formControls col-xs-9 col-sm-9">
				<input type="text" class="input-text '.$class.'" style="margin-bottom: 15px;'.$style.'" autocomplete="off" value="'.$value.'" name="'.$name.'">
			</div>';
        }

        return $this;
    }
    /**添加隐藏框
     * @param字段名称 $name
     * @param 值 $value
     * @return $this
     */
    public function addNone($name,$value=''){
        $this->str .= '<input type="text" name="'.$name.'" value="'.$value.'" style="display:none">';
        return $this;
    }

    /**添加多行文本
     * @param 字段名称 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addTextarea($name,$desc='',$value='',$class=null,$style=null){
        $this->str .= '<div class="layui-row" style="margin-top: 15px">
			<label class="form-label col-xs-4 col-sm-3" style="text-align: right"><span class="c-red">*</span>'.$desc.'：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<textarea name="'.$name.'" class="'.$class.'" style="margin-bottom: 15px;height:200px;'.$style.'">'.$value.'</textarea>
			</div>';
        return $this;
    }

    /**添加下拉框
     * @param 字段名称 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param array选项 $option
     * @param null样式 $class
     * @param null样式 $style
     * @param bool是否键值一样 $identical
     * @return $this
     */
    public function addSelect($name,$desc='',$value='',$option=[],$class=null,$style=null,$identical=false){
        $this->str .= '<div class="layui-row" style="margin-top: 15px">
			<label class="form-label col-xs-4 col-sm-3" style="text-align: right"><span class="c-red">*</span>'.$desc.'：</label>
			<div class="formControls col-xs-8 col-sm-9"><select name="'.$name.'" class="layui-select;'.$class.'" style="margin-bottom: 15px;height:25px;border:1 sild'.$style.'">';
        $this->str.='<option value="">请选择</option>';
				foreach ($option as $key=>$values){
				    if($identical){
                        if($value==$values){
                            $this->str.='<option value="'.$values.'" selected>'.$values.'</option>';
                        }else{
                            $this->str.='<option value="'.$values.'">'.$values.'</option>';
                        }
                    }else{
                        if($value==$key){
                            $this->str.='<option value="'.$key.'" selected>'.$values.'</option>';
                        }else{
                            $this->str.='<option value="'.$key.'">'.$values.'</option>';
                        }
                    }

                }
			$this->str.='</select></div></div>';
        return $this;
    }
    public function addLinkageSelect($name,$desc='',$cengji=1,$action='',$option=[],$class=null,$style=null){
            $this->str .= '<div class="layui-row">
			<label class="form-label col-xs-4 col-sm-3" style="text-align: right"><span class="c-red">*</span>'.$desc.'：</label>
			<div class="formControls col-xs-8 col-sm-9 '.$class.'"  style="'.$style.'">';
            for ($a=1;$a<=$cengji;$a++){
                if($a==$cengji){
                    $this->str .= '<select class="layui-select" onchange="getLinkageSelect(this,\''.$action.'\')" name="'.$name.'" style="width: 30%;margin-top: 15px;'.$style.'"><option value="">请选择</option></select><br>';
                }elseif($a==1){
                    $this->str .= '<select class="layui-select" onchange="getLinkageSelect(this,\''.$action.'\')" style="width: 30%;'.$style.'"><option value="">请选择</option>';
                    foreach ($option as $key=>$value){
                        $this->str .= '<option value="'.$value['id'].'">'.$value['name'].'</option>';
                    }
                    $this->str.='</select><br>';
                }else{
                    $this->str .= '<select class="layui-select"  onchange="getLinkageSelect(this,\''.$action.'\')"  style="width: 30%;margin-top: 15px;'.$style.'"><option value="">请选择</option></select><br>';
                }
            }
            $this->str .= '</div></div>';
        return $this;
    }
    /**添加图片框
     * @param 字段名称 $name
     * @param string描述 $desc
     * @param null值 $value
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     *
     */
    public function addImg($name,$desc='',$value=null,$class=null,$style=null){
        $this->str .= '<div class="layui-row" style="margin-top: 15px">
			            <label class="form-label col-xs-4 col-sm-3" style="text-align: right"><span class="c-red">*</span>'.$desc.'：</label>
                            <div class="formControls col-xs-8 col-sm-9 file" style="width: 90px">
                            <input name="'.$name.'" style="display:none" value="'.$value.'">
                                <input type="file" class="'.$class.'" style="margin-bottom: 15px;width:100%'.$style.'" name="maowenke" onchange="yulan(this)" autocomplete="off">点击预览
                            </div>
                       </div>
			    <div class="layui-row">
			<label class="form-label col-xs-4 col-sm-3" style="text-align: right"><span class="c-red"></span>&nbsp;</label>
			<div class="formControls col-xs-8 col-sm-9 show_img">';
        if($value){
            $values = getFilePath($value);
            $this->str .= '<div style="text-align: center;float: left;margin: 5px"><img src="'.$values.'" width="150"/><br><input class="layui-btn layui-btn-xs layui-btn-danger" style="margin-top: 5px" onclick="delimg(this,'.$value.')" type="button" value="删除"></div>';
        }
			$this->str .='</div></div>';
        return $this;
    }

    /**添加多图片
     * @param 字段名称$name
     * @param string描述 $desc
     * @param string值 $value
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addImgs($name,$desc='',$value='',$class=null,$style=null){
        $this->str .= '<div class="layui-row" style="margin-top: 15px">
			        <label class="form-label col-xs-4 col-sm-3" style="text-align: right"><span class="c-red">*</span>'.$desc.'：</label>
                        <div class="formControls col-xs-8 col-sm-9 file" style="width: 90px">
                        <input type="text" name="'.$name.'" style="display:none" value="'.$value.'">
                            <input type="file" class="input-text '.$class.'" style="margin-bottom: 15px;width:100%'.$style.'" name="maowenke" onchange="yulan(this)" autocomplete="off" multiple>点击预览
                        </div>
                        </div>
               <div class="layui-row">
			<label class="form-label col-xs-4 col-sm-3" style="text-align: right"><span class="c-red"></span>&nbsp;</label>
			<div class="formControls col-xs-8 col-sm-9 show_img">';
        if($value){
                $values = strGoArray($value);
            foreach ($values as  $v){
                $this->str .= '<div style="text-align: center;float: left;margin: 5px"><img src="'.getFilePath($v).'" width="150"/><br><input class="layui-btn layui-btn-xs layui-btn-danger" style="margin-top: 5px" onclick="delimg(this,'.$value.')" type="button" value="删除"></div>';
            }
        }
        $this->str .='</div></div>';
        return $this;
    }

    /**添加单文件
     * @param 字段名称 $name
     * @param string描述 $desc
     * @param null值 $value
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addFile($name,$desc='',$value=null,$class=null,$style=null){
        $this->str .= '<div class="layui-row">
			<label class="form-label col-xs-4 col-sm-3" style="text-align: right"><span class="c-red">*</span>'.$desc.'：</label>
			<div class="formControls col-xs-8 col-sm-9"><input type="file" class="'.$class.'" style="margin-bottom: 15px;width:150px'.$style.'" value="'.$value.'" autocomplete="off" name="'.$name.'"></div>';
        return $this;
    }
    /**添加多文件
     * @param 字段名称 $name
     * @param string描述 $desc
     * @param null值 $value
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addFiles($name,$desc='',$value='',$class=null,$style=null){
        $this->str .= '<div class="layui-row" style="margin-top: 15px">
			<label class="form-label col-xs-4 col-sm-3" style="text-align: right"><span class="c-red">*</span>'.$desc.'：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="file" class="'.$class.'" style="margin-bottom: 15px;'.$style.'" autocomplete="off" name="'.$name.'[]" value="'.$value.'" multiple>
			</div>';
        return $this;
    }

    /**添加单选框
     * @param 字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param array选项 $option
     * @param null样式 $class
     * @param null样式 $style
     * @param bool是否值键一样 $identical
     * @return $this
     */
    public function addRadio($name,$desc='',$value='',$option=[],$class=null,$style=null,$identical=false){
        $this->str .= '<div class="layui-row" style="margin-top: 15px">
			<label class="form-label col-xs-4 col-sm-3" style="text-align: right"><span class="c-red">*</span>'.$desc.'：</label>
			<div class="formControls col-xs-8 col-sm-9" style="margin-bottom: 15px;">';
        foreach ($option as $key=>$values){
            if($identical){
                if($value==$values){
                    $this->str .= '<input type="radio" class="'.$class.'" style="width:15px;'.$style.'" autocomplete="off" name="'.$name.'" value="'.$values.'" checked>'.$values;
                }else{
                    $this->str .= '<input type="radio" class="'.$class.'" style="'.$style.'" autocomplete="off" name="'.$name.'" value="'.$values.'">'.$values;
                }
            }else{
                if($value==$key){
                    $this->str .= '<input type="radio" class="'.$class.'" style="'.$style.'" autocomplete="off" name="'.$name.'" value="'.$key.'" checked>'.$values;
                }else{
                    $this->str .= '<input type="radio" class="'.$class.'" style="'.$style.'" autocomplete="off" name="'.$name.'" value="'.$key.'">'.$values;
                }
            }
        }
			$this->str.='</div>';
        return $this;
    }
    /**添加复选框
     * @param 字段名 $name
     * @param string描述 $desc
     * @param string值 $value
     * @param array选项 $option
     * @param null样式 $class
     * @param null样式 $style
     * @param bool是否值键一样 $identical
     * @return $this
     */
    public function addCheckbox($name,$desc='',$value=[],$option=[],$class=null,$style=null,$identical=false){
        $this->str.='<div class="layui-row" style="margin-top: 15px">
			<label class="form-label col-xs-4 col-sm-3" style="text-align: right"><span class="c-red">*</span>'.$desc.'：</label>
			<div class="formControls col-xs-8 col-sm-9" style="margin-bottom: 15px;">';
        foreach ($option as $key=>$values){
            $a = 0;
            if($identical){
                if($value&&is_array($value)){
                    foreach ($value as $b){
                        if($b==$values){
                            $a=1;
                        }
                    }
                }
                if($a==1){
                    $this->str.='<input type="checkbox" class="'.$class.'" style="'.$style.'" autocomplete="off" name="'.$name.'[]" value="'.$values.'" checked>'.$values;
                }else{
                    $this->str.='<input type="checkbox" class="'.$class.'" style="'.$style.'" autocomplete="off" name="'.$name.'[]" value="'.$values.'">'.$values;
                }
            }else{
                    foreach ($value as $b){
                        if($b==$key){
                            $a=1;
                        }
                    }
                if($a==1){
                    $this->str.='<input type="checkbox" class="input-text '.$class.'" style="'.$style.'" autocomplete="off" name="'.$name.'[]" value="'.$key.'" checked>'.$values;
                }else{
                    $this->str.='<input type="checkbox" class="input-text '.$class.'" style="'.$style.'" autocomplete="off" name="'.$name.'[]" value="'.$key.'">'.$values;
                }
            }
        }
			$this->str.='</div>';
        return $this;
    }

    /**添加编辑框
     * @param 字段名称$name
     * @param string描述 $desc
     * @param string值 $value
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addEditor($name,$desc='',$value='',$class=null,$style=null){
        $this->str .= '<div class="layui-row" style="margin-top: 15px">
		<label class="form-label col-xs-4 col-sm-3" style="text-align: right">
			<span class="c-red">*</span>' . $desc . '：
		</label>
		<div class="formControls col-xs-8 col-sm-9 '.$class.'" style="margin-bottom: 15px;'.$style.'">
			<script id="editor'.$name.'" type="text/plain" name="'.$name.'" style="width:100%;height:500px;">'.$value.'</script>
			<script>
    var '.$name.' = UE.getEditor(\'editor'.$name.'\');
</script>
		</div>
	</div><br>';
        return $this;
    }

    /**添加开关
     * @param 字段名称$name
     * @param string描述 $desc
     * @param string值 $value
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addSwitch($name,$desc='',$value='',$class=null,$style=null){
        $this->str.='<div class="layui-row" style="margin-top: 15px">
			<label class="form-label col-xs-4 col-sm-3" style="text-align: right"><span class="c-red">*</span>'.$desc.'：</label>
			<div class="formControls col-xs-8 col-sm-9" style="margin-bottom: 15px;">';
        if($value==1){
            $this->str.='<div class="layui-input-block"><input type="checkbox" name="'.$name.'" lay-skin="switch" value="'.$value.'" class=" '.$class.'" style="'.$style.'" checked> </div>';
        }else{
            $this->str.='<div class="layui-input-block"><input type="checkbox" name="'.$name.'" lay-skin="switch" value="0" class="'.$class.'" style="'.$style.'"></div>';
        }
			$this->str.='</div>';
        return $this;
    }

    /**添加分页，添加id地址
     * @param $id
     */
public function addPage($id){
        $this->last = '<div style="text-align: center"><div id="'.$id.'"></div></div>';
        return $this;
}
    /**添加按钮
     * @param array描述 $desc
     * @param array类型 $type
     * @param null样式 $class
     * @param null样式 $style
     * @return $this
     */
    public function addButton($desc=[],$type=[],$class=null,$style=null){
        $this->str .='<div class="layui-row" style="margin-top: 15px">
<label class="form-label col-xs-4 col-sm-3" style="text-align: right"><span class="c-red"></span>&nbsp;</label>
		<div class="formControls col-xs-8 col-sm-9" style="margin-left: -20px">';
        foreach ($desc as $key=>$value){
            if($type[$key]=='submit'){
                $this->str .='<input type="'.$type[$key].'" value="'.$value.'" class="btn btn-primary radius '.$class.'" style="float:left;margin-left: 15px;margin-bottom: 50px;'.$style.'"/>';
            }elseif ($type[$key]=='back'){
                $this->str .='<input type="button" value="'.$value.'" onclick="history.back(-1);" class="btn '.$class.'" style="float:left;margin-left: 15px;margin-bottom: 50px;'.$style.'"/>';
            }
        }
        $this->str.='</div>
	</div>';
        return $this;
    }

    /**返回字符串
     * @return string
     */
    public function fetch(){
        if($this->type=='table'){
            $this->str = $this->first.$this->start.'<thead><tr>'.$this->head.'</tr></thead><tbody>'.$this->str.'</tbody>'.$this->end.$this->last;
        }else{
            $this->str = $this->start.$this->str.$this->end;
        }
        return $this->str;
    }
}